---
title: Promises
tags: [examples, promise, beforeAll, afterAll, tutorial, roadmap]
sidebar_position: 0
---

import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
import MidLevel from '@site/static/img/mid-level.svg';

# Promises

The use of [native **JavaScript** syntax in tests](/docs/2.x.x/philosophy#javascript-essence-for-tests-) is one of the major differences between **Poku** and other test runners, which is what makes it possible to use it on multiple platforms.

:::tip
It's not necessary to use `await` for tests that aren't asynchronous.
:::

Here are some examples of sequential and concurrent tests in the same file plus how to perform an action after all the tests have been completed:

### Running async tests in the same file in parallel

```js
import { test, assert, sleep } from 'poku';

test(async () => {
  const actual = 1;
  const expected = 1;

  await sleep(2000);

  assert.strictEqual(actual, expected);
});

test(async () => {
  const actual = 2;
  const expected = 2;

  await sleep(1000);

  assert.strictEqual(actual, expected);
});
```

<hr />

### Running async tests in the same file sequentially (await top-level)

```js
import { test, assert, sleep } from 'poku';

await test(async () => {
  const actual = 1;
  const expected = 1;

  await sleep(2000);

  assert.strictEqual(actual, expected);
});

await test(async () => {
  const actual = 2;
  const expected = 2;

  await sleep(1000);

  assert.strictEqual(actual, expected);
});
```

<hr />

### Running async tests in the same file sequentially

```js
import { describe, it, assert, sleep } from 'poku';

describe(async () => {
  await it(async () => {
    const actual = 1;
    const expected = 1;

    await sleep(2000);

    assert.strictEqual(actual, expected);
  });

  await it(async () => {
    const actual = 2;
    const expected = 2;

    await sleep(1000);

    assert.strictEqual(actual, expected);
  });
});
```

<hr />

## Waiting for all the tests to run a post step

```js
import { describe, it, assert, sleep } from 'poku';

describe(async () => {
  console.log('Printing it before all tests 🏃🏻‍♀️');

  await Promise.all([
    test(async () => {
      const actual = 1;
      const expected = 1;

      await sleep(2000);

      assert.strictEqual(actual, expected);
    }),

    test(async () => {
      const actual = 2;
      const expected = 2;

      await sleep(1000);

      assert.strictEqual(actual, expected);
    }),
  ]);

  console.log('Printing it after all tests 😴');
});
```

:::tip
For multiple asynchronous `describe` or `test`, you can also use `await` to run them sequentially.
:::

<hr />

:::danger Be careful
When using asynchronous `beforeEach` or `afterEach`, it's necessary to use `await` even if the `test` or `it` helpers doesn't have any asynchronous events.
:::

:::note
If you find any typos, feel free to open a **Pull Request** correcting them.
:::
